package com.qyhl.module_practice.score.shop.detail;


import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeShopListBean;

public class PracticeScoreShopDetailModel implements PracticeScoreShopDetailContract.PracticeScoreShopDetailModel {
    private PracticeScoreShopDetailPresenter mPresenter;

    PracticeScoreShopDetailModel(PracticeScoreShopDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String itemId) {
        EasyHttp.get(PracticeUrl.GET_PRACTICE_SCORE_SHOPPING_DETAIL)
                .params("itemId", itemId)
                .execute(new SimpleCallBack<PracticeShopListBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无该商品详情内容！");
                        } else {
                            mPresenter.setError("获取详情失败，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(PracticeShopListBean practiceShopListBean) {
                        mPresenter.setDetail(practiceShopListBean);
                    }
                });
    }

    @Override
    public void doExchange(String itemId, String volId, String phone) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_SCORE_EXCHANGE)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("itemId", itemId)
                .params("volId", volId)
                .params("phone", phone)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.exchangeError(-1);
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.exchangeSuccess("兑换成功，请在我兑换的商品中查收！");
                        } else {
                            mPresenter.exchangeError(apiResult.getCode());
                        }
                    }
                });

    }
}
